<?php

define('IN_PHPBB', true);
$phpbb_root_path = (defined('PHPBB_ROOT_PATH')) ? PHPBB_ROOT_PATH : './';
$phpEx = substr(strrchr(__FILE__, '.'), 1);
include($phpbb_root_path . 'common.' . $phpEx);

// Start session management
$user->session_begin();
$auth->acl($user->data);
$user->setup('mods/social_network_mod');

$sn_config = $sn->get_sn_config();

$mode = request_var('mode', '');
$submit = request_var('submit', '');

switch ($mode)
{
	case 'create':
		// We don't want bots or unregistered users here.
		if ($user->data['user_id'] == ANONYMOUS)
		{
			login_box('', $user->lang['LOGIN']);
		}
		if ($user->data['is_bot'])
		{
			redirect(append_sid("{$phpbb_root_path}index.$phpEx"));
		}

		if ($submit)
		{
			// Get the info for the club and create it
			$club_ary = array(
				'club_name'		=> request_var('club_name', ''),
				'club_email'	=> request_var('email', ''),
				'club_email2'	=> request_var('email_confirm', ''),
				'club_desc'		=> request_var('club_desc', ''),
				//'club_reason'	=> ($sn_config['clubs_verification']) ? request_var('club_reason', '') : '',
				'club_reason'	=> request_var('club_reason', ''),
				'club_founder'	=> $user->data['user_id'],
			);

			$sn->create_club($club_ary);

			$redirect = append_sid("{$phpbb_root_path}clubs.$phpEx");
			meta_refresh(3, $redirect);
			trigger_error($user->lang['CLUB_CREATED']);
		}
		else
		{
			$template->assign_vars(array(
				'CREATE_CLUB'					=> true,

				'S_CLUB_ACTION'					=> append_sid("{$phpbb_root_path}clubs.$phpEx", 'mode=create'),
				//'S_ADMIN_VERIFICATION'			=> $clubs_config['clubs_verification'],
				'S_ADMIN_VERIFICATION'			=> 1,
			));
		}

		$page_title = $user->lang['CLUBS_REGISTER_TITLE'];
	break;

	case 'info':
		$club_id = request_var('cid', '');
		$club_info = $sn->get_club_info($club_id);

		$sql = 'SELECT * FROM ' . USERS_TABLE . '
			WHERE user_id = ' . $club_info['club_founder'];
		$result = $db->sql_query($sql);
		$user_info = $db->sql_fetchrow($result);
		$db->sql_freeresult($result);

		$template->assign_vars(array(
			'S_CLUB_INFO'						=> true,

			'CLUB_ID'							=> $club_info['club_id'],
			'CLUB_NAME'							=> $club_info['club_name'],
			'CLUB_DESC'							=> $club_info['club_desc'],
			'MEMBERS'							=> $club_info['num_members'],
			'CREATED'							=> $user->format_date($club_info['creation_time']),
			'CLUB_COMMENTS'						=> $club_info['num_comments'],
			'CLUB_FOUNDER'						=> get_username_string('full', $club_info['club_founder'], $user_info['username'], $user_info['user_colour']),
			//'LAST_CLUB_COMMENTER_FULL'			=> $club_info['last_commenter'],
			//'U_LAST_CLUB_COMMENT'				=> append_sid("{$phpbb_root_path}clubs.$phpEx", 'mode=last_comment&amp;club=' . $club_id),
			//'LAST_CLUB_COMMENT_TIME'			=> $club_info['last_comment_time'],

			'CLUB_PROFILE_TITLE'				=> sprintf($user->lang['CLUB_PROFILE_TITLE'], $club_info['club_name']),

			'U_CLUB_EMAIL'						=> 'mailto:' . $club_info['club_email'],
			'U_CLUB_JOIN'						=> append_sid("{$phpbb_root_path}clubs.$phpEx", 'mode=join&amp;cid=' . $club_info['club_id']),
		));

		$page_title = sprintf($user->lang['CLUB_INFO_TITLE'], $club_info['club_name']);
	break;

	case 'join':
		$cid = request_var('cid', 0);
		$club_info = $sn->get_club_info($cid);
		$page_title = sprintf($user->lang['CLUB_JOIN_TITLE'], $club_info['club_name']);

		$template->assign_var('S_CLUB_JOIN', true);

		if ($submit)
		{
			$sql_ary = array(
				'club_id'		=> request_var('cid', 0),
				'user_id'		=> $user->data['user_id'],
				'join_reason'	=> request_var('join_reason', ''),
			);

			$sql = 'INSERT INTO ' . SN_CLUBS_PENDING_TABLE . ' ' . $db->sql_build_array('INSERT', $sql_ary);
			$db->sql_query($sql);

			$redirect = append_sid("{$phpbb_root_path}clubs.$phpEx", 'mode=info&amp;cid=' . $cid);
			meta_refresh(3, $redirect);
			trigger_error($user->lang['REQUEST_SENT']);
		}
		else
		{
			$cid = request_var('cid', 0);

			$sql = 'SELECT * FROM ' . SN_CLUBS_TABLE . '
				WHERE club_id = ' . $cid;
			$result = $db->sql_query($sql);
			$row = $db->sql_fetchrow($result);
			$db->sql_freeresult($result);

			$template->assign_vars(array(
				'S_JOIN_ACTION'				=> append_sid("{$phpbb_root_path}clubs.$phpEx", 'mode=join&amp;cid=' . $cid),
				'S_JOIN_CLUB'				=> sprintf($user->lang['JOIN_CLUB'], $row['club_name']),
			));
		}
	break;

	case 'comment':
		if ($submit)
		{
		}
		else
		{
		}

		$page_title = $user->lang['CLUB_COMMENT_POSTING'];
	break;

	default:
		$clubs_list = $sn->get_clubs_list();

		$template->assign_vars(array(
			'CLUBS_LIST'						=> true,

			'U_CREATE_CLUB'						=> append_sid("{$phpbb_root_path}clubs.$phpEx", 'mode=create'),
		));

		$page_title = $user->lang['CLUBS_LIST_TITLE'];
	break;
}

$template->assign_vars(array(
));

page_header($page_title);

$template->set_filenames(array(
	'body' => 'sn/sn_clubs_body.html',
));

page_footer();

?>